package com.freshfresh.adapter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.text.SpannableString;
import android.text.style.StrikethroughSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.freshfresh.activity.R;
import com.freshfresh.activity.classification.ProductDetailsActivity;
import com.freshfresh.utils.ImageLoadOptions;
import com.freshfresh.utils.StringUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

public class IndexListItem implements View.OnClickListener
{
  private final String TYPE_ID = "305";
  private Context context;
  private int hour;
  private long interval;
  private Map<String,Object> list;
  private int minute;
  private int second;

  private Date convertStrToDate(String paramString)
  {
    SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try
    {
      Date localDate = localSimpleDateFormat.parse(paramString);
      return localDate;
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    return null;
  }

  private boolean datesOffset(String paramString)
  {
    Date localDate = convertStrToDate(paramString);
    if (localDate == null)
      return false;
    this.interval = (localDate.getTime() - new Date().getTime());
    this.hour = new Long(this.interval / 3600000L).intValue();
    int i = new Long(this.interval % 3600000L).intValue();
    this.minute = new Long(i / 60000).intValue();
    this.second = new Long(new Long(i % 60000).intValue() / 1000).intValue();
    return true;
  }

  private View getItemView(Map<String,Object> paramIndexProInfoListItem){
  
    View localView = LayoutInflater.from(this.context).inflate(R.layout.index_list_item3, null);
    loadImg((ImageView)localView.findViewById(R.id.iv_proImg), paramIndexProInfoListItem.get("image").toString().replace("", ""));
    loadImg((ImageView)localView.findViewById(R.id.iv_guoqi), paramIndexProInfoListItem.get("national_flag").toString());
    TextView localTextView1 = (TextView)localView.findViewById(R.id.tv_proType);
    localTextView1.setText(this.list.get("category_name").toString());
    if ("305".equals(this.list.get("categoryid").toString()))
    {
      TextView localTextView3 = (TextView)localView.findViewById(R.id.tv_shengyushijian);
//      if (datesOffset(paramIndexProInfoListItem.getSpecial_price_to_date()))
//        new TimeCount(this.interval, 1000L, localTextView3).start();
      localTextView1.setBackgroundResource(R.drawable.jiaobiao);
    }
    while (true)
    {
      ((TextView)localView.findViewById(R.id.tv_proName)).setText(paramIndexProInfoListItem.get("name").toString());
      ((TextView)localView.findViewById(R.id.tv_standard)).setText(paramIndexProInfoListItem.get("standard2").toString());
      TextView localTextView2 = (TextView)localView.findViewById(R.id.tv_originalPrice);
      String str = "原价" + StringUtil.formatPrice(paramIndexProInfoListItem.get("price").toString());
      SpannableString localSpannableString = new SpannableString(str);
      localSpannableString.setSpan(new StrikethroughSpan(), 0, str.length(), 33);
      localTextView2.setText(localSpannableString);
      ((TextView)localView.findViewById(R.id.tv_price)).setText(StringUtil.formatPrice("0.00", paramIndexProInfoListItem.get("special_price").toString()));
//      (TextView)(localView.findViewById(R.id.btn_buy));
      localView.setOnClickListener(this);
      localView.setTag(paramIndexProInfoListItem.get("product_id").toString() + "," + paramIndexProInfoListItem.get("sku").toString() + "," + this.list.get("categoryid").toString());
      localTextView1.setBackgroundResource(R.drawable.jiaobiao2);
      return localView;
    }
  }

  private void loadImg(final ImageView paramImageView, String paramString)
  {
    if ((paramString == null) || ("".equals(paramString)))
      return;
    ImageLoader.getInstance().displayImage(paramString, paramImageView, ImageLoadOptions.getOptions(0));
  }

  public View getView(Context paramContext, Map<String,Object> paramIndexProInfo)
  {
    this.context = paramContext;
    this.list = paramIndexProInfo;
    LinearLayout localLinearLayout = new LinearLayout(paramContext);
//    ListView.LayoutParams localLayoutParams = new ListView.LayoutParams(-1, -2);
    localLinearLayout.setOrientation(1);
//    localLinearLayout.setLayoutParams(localLayoutParams);
    Iterator localIterator = ((List<Map<String,Object>>)(((Map<String,Object>)paramIndexProInfo.get("product_list")).get("product_info"))).iterator();
    while (true)
    {
      if (!localIterator.hasNext())
        return localLinearLayout;
      localLinearLayout.addView(getItemView((Map<String,Object>)localIterator.next()));
    }
  }

  public void onClick(View paramView)
  {
    String[] arrayOfString = paramView.getTag().toString().split(",");
    Intent localIntent = new Intent(this.context, ProductDetailsActivity.class);
    localIntent.putExtra("productid", arrayOfString[0]);
    localIntent.putExtra("sku", arrayOfString[1]);
    localIntent.putExtra("categoryid", arrayOfString[2]);
    this.context.startActivity(localIntent);
  }

//  private class TimeCount extends CountDownTimer
//  {
//    private TextView tv;
//
//    public TimeCount(long arg2, long arg4, TextView arg6)
//    {
//      super(localObject1);
//      Object localObject2;
//      this.tv = localObject2;
//    }
//
//    public void onFinish()
//    {
//    }
//
//    public void onTick(long paramLong)
//    {
//      if (IndexListItem.this.second == 0)
//        if (IndexListItem.this.minute == 0)
//        {
//          IndexListItem localIndexListItem3 = IndexListItem.this;
//          localIndexListItem3.hour = (-1 + localIndexListItem3.hour);
//          IndexListItem.this.minute = 59;
//        }
//      IndexListItem localIndexListItem1;
//      for (IndexListItem.this.second = 59; ; localIndexListItem1.second = (-1 + localIndexListItem1.second))
//      {
//        this.tv.setText("剩余" + IndexListItem.this.hour + "时" + IndexListItem.this.minute + "分" + IndexListItem.this.second + "秒");
//        return;
//        IndexListItem localIndexListItem2 = IndexListItem.this;
//        localIndexListItem2.minute = (-1 + localIndexListItem2.minute);
//        break;
//        localIndexListItem1 = IndexListItem.this;
//      }
//    }
//  }
}